<?php
/**
 * Created by PhpStorm.
 * User: evenvi
 * Date: 16-7-2
 * Time: 上午10:48
 * Contact: ${Contact}
 */

namespace ODP\Util;


class Util
{
    /**
     * 获取HTTP头信息
     * @return array
     */
    public static function get_all_headers()
    {
        $headers = array();
        foreach ($_SERVER as $name => $value)
        {
            if (substr($name, 0, 5) == 'HTTP_')
            {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }

    /**
     * 获取当前时间的毫秒数
     * @return float
     */
    public static function get_millisecond()
    {
        list($t1, $t2) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
    }

    public static function read_file_dir($dir, &$data_files=array())
    {
        if(!is_dir($dir))
            return false;
        $handle=opendir($dir);
        while(($file=readdir($handle))!==false)
        {

            if($file=='.'||$file=='..')
            {
                continue;
            }
            $file=$dir.DIRECTORY_SEPARATOR.$file;
            if(is_file($file))                 //是文件就输出
            {
                array_push($data_files, $file);
            }
            elseif(is_dir($file))
            {

                self::read_file_dir($file, $data_files);
            }
        }
        closedir($handle);
    }

    public static function curl_post($data, $url)
    {
        $data = gzencode($data);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url); //定义表单提交地址
        curl_setopt($ch, CURLOPT_POST, 1);   //定义提交类型 1：POST ；0：GET
        curl_setopt($ch, CURLOPT_HEADER, 1); //定义是否显示状态头 1：显示 ； 0：不显示
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//定义是否直接输出返回流,0直接,1则返回
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //定义提交的数据
//        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
//        curl_setopt($ch, CURLOPT_ENCODING, "gzip");

        $res = curl_exec($ch);

//        echo strlen($data);
//        var_dump($res); die;
//        var_dump(curl_getinfo($ch)); die;
        curl_close($ch);//关闭
        return $res;
    }
}